home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module10- qtzoo / completed source / animalpane.java next >
Encoding:
Java Source  |  2000-09-28  |  14.8 KB  |  481 lines

  1. import java.awt.Dimension;
  2. import java.io.IOException;
  3. import java.io.FileNotFoundException;
  4. import java.util.Hashtable;
  5.  
  6. import quicktime.app.QTFactory;
  7. import quicktime.app.anim.Compositor;
  8. import quicktime.app.anim.TwoDSprite;
  9. import quicktime.app.event.QTActionEvent;  
  10. import quicktime.app.event.QTActionListener;
  11. import quicktime.app.event.QTMouseTargetController; 
  12. import quicktime.app.image.GraphicsImporterDrawer; 
  13. import quicktime.app.image.ImagePresenter; 
  14. import quicktime.app.image.ImageDataSequence;
  15. import quicktime.app.image.ImageUtil;
  16. import quicktime.app.ui.ReleaseButton;
  17. import quicktime.app.ui.ButtonActivator;
  18. import quicktime.app.players.MoviePresenter;
  19. import quicktime.app.players.QTPlayer;
  20.  
  21. import quicktime.io.QTFile;
  22. import quicktime.io.OpenMovieFile;
  23.  
  24. import quicktime.qd.QDRect;
  25. import quicktime.qd.QDGraphics;
  26. import quicktime.qd.QDColor; 
  27. import quicktime.qd.QDConstants; 
  28.  
  29. import quicktime.std.StdQTConstants;
  30. import quicktime.std.movies.Movie; 
  31. import quicktime.std.image.Matrix;
  32. import quicktime.std.image.GraphicsMode;
  33.  
  34. import quicktime.QTSession;
  35. import quicktime.QTException; 
  36.  
  37. /**
  38.  * QTZoo Module 10 - Putting it all together
  39.  *
  40.  * @author Levi Brown
  41.  * @author Michael Hopkins
  42.  * @author Apple Computer, Inc.
  43.  * @version 8.0 4/10/2000
  44.  * 
  45.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  46.  *    
  47.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  48.  *                ("Apple") in consideration of your agreement to the following terms, and your
  49.  *                use, installation, modification or redistribution of this Apple software
  50.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  51.  *                please do not use, install, modify or redistribute this Apple software.
  52.  *
  53.  *                In consideration of your agreement to abide by the following terms, and subject
  54.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  55.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  56.  *                reproduce, modify and redistribute the Apple Software, with or without
  57.  *                modifications, in source and/or binary forms; provided that if you redistribute
  58.  *                the Apple Software in its entirety and without modifications, you must retain
  59.  *                this notice and the following text and disclaimers in all such redistributions of
  60.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  61.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  62.  *                Apple Software without specific prior written permission from Apple.  Except as
  63.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  64.  *                are granted by Apple herein, including but not limited to any patent rights that
  65.  *                may be infringed by your derivative works or by other works in which the Apple
  66.  *                Software may be incorporated.
  67.  *
  68.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  69.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  70.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  71.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  72.  *                COMBINATION WITH YOUR PRODUCTS.
  73.  *
  74.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  75.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  76.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  77.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  78.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  79.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  80.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  81.  * 
  82.  */
  83. public class AnimalPane extends ZooPane
  84. {
  85.     /**
  86.      *  Public default constructor
  87.      *  Creates the map button, and sets up the listeners
  88.      */
  89.     public AnimalPane()
  90.     {
  91.         player = null;
  92.  
  93.         QDRect size = new QDRect(MainFrame.WIDTH, MainFrame.HEIGHT);
  94.         try
  95.         {
  96.             QDGraphics gw = new QDGraphics (size);
  97.             compositor = new Compositor (gw, QDColor.white, 30, 1);
  98.             
  99.             ctr = new QTMouseTargetController( false );
  100.             ctr.addQTMouseListener( new PaneMouseListener( QDColor.lightGray, QDColor.white, QDColor.darkGray));
  101.             compositor.addController( ctr );            
  102.                 
  103.              TwoDSprite currentSprite;
  104.              areas = new Hashtable(1);
  105.             currentSprite = CreateRollover( "data/MapButton.jpg", 5, 12, 406 );
  106.             areas.put(currentSprite, "map");    
  107.             
  108.             compositor.getTimer().setRate(0);    
  109.         }
  110.         catch ( QTException qte )
  111.         {
  112.             qte.printStackTrace();
  113.         }
  114.     }    
  115.     
  116.     /**
  117.      * Listener object for the Movie controller buttons
  118.      * Called when the button is released
  119.      */
  120.     public class ButtonListener implements QTActionListener    
  121.     {
  122.         /**
  123.          * Called when the button is released
  124.          * @param ???
  125.          */
  126.         public void actionPerformed( QTActionEvent event ) 
  127.         {
  128.             System.out.println( "AnimalPane.actionPerformed() " );
  129.             try
  130.             {
  131.                 Object source = event.getSource();
  132.                 
  133.                 if (source.equals(playButton))            // Play >
  134.                 {
  135.                     if ( md.getRate() == 0 )
  136.                     {
  137.                         md.setRate( 1 );
  138.                     }
  139.                     stopButton.setCurrentImage( stopRel );
  140.                     stopButton.setReleasedImage( stopRel );
  141.                     playButton.setCurrentImage( playPlaying );
  142.                     playButton.setReleasedImage( playPlaying );
  143.                 }                
  144.                 else if (source.equals(stopButton))        // Stop []
  145.                 {
  146.                     if ( md.getRate() == 1 )
  147.                     {
  148.                         md.setRate( 0 );
  149.                     }
  150.                     stopButton.setCurrentImage( stopDeactive );
  151.                     stopButton.setReleasedImage( stopDeactive );
  152.                     playButton.setCurrentImage( playRel );
  153.                     playButton.setReleasedImage( playRel );
  154.                 }
  155.                 else if (source.equals(rewindButton))    // Rewind <<
  156.                 {
  157.                     md.setTime(0);
  158.                 }
  159.             }
  160.             catch (QTException exc )
  161.             {
  162.                 exc.printStackTrace();
  163.             }
  164.         }
  165.     }
  166.     
  167.     /**
  168.      * Loads a graphics file from a relative path and creates an ImagePresenter object
  169.      * @param ???
  170.      */
  171.     public ImagePresenter MakePresenterFromFile( String name )
  172.     {
  173.         try
  174.         {
  175.             QTFile file = new QTFile (QTFactory.findAbsolutePath (name));
  176.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer(file);
  177.             return ImagePresenter.fromGraphicsImporterDrawer(drawer);
  178.         }
  179.         catch (IOException e)
  180.         {
  181.             e.printStackTrace();
  182.         }
  183.         catch (QTException e)
  184.         {
  185.             e.printStackTrace();
  186.         }
  187.         return null;    
  188.     }
  189.     
  190.     /**
  191.      * Called to do any setup after being set as the client of the QTCanvas.
  192.      * May be used to start effects running, movies playing, etc.
  193.      */
  194.     public void start()
  195.     {
  196.         buttonController.addQTMouseListener(buttonActivator);
  197.         try
  198.         {
  199.             stopButton.setCurrentImage(  stopRel );        // Stop button should not have focus
  200.             playButton.setCurrentImage(  playPlaying );  // Play button should get focus (blue arrow)
  201.             playButton.setReleasedImage( playPlaying );
  202.         }
  203.         catch( QTException e )
  204.         {
  205.             e.printStackTrace();
  206.         }
  207.         try
  208.         {
  209.             md.setRate(1);                                // start the player
  210.         }
  211.         catch( QTException e )
  212.         {
  213.             e.printStackTrace();
  214.         }
  215.         compositor.getTimer().setRate(1);                // start the compositor
  216.     }
  217.     
  218.     /**
  219.      * Called to do clean up after being removed as the client of the QTCanvas.
  220.      * Should be used to stop effects running, movies playing, etc.
  221.      */
  222.     public void stop()
  223.     {
  224.         buttonController.removeQTMouseListener(buttonActivator);
  225.         compositor.getTimer().setRate(0);
  226.         try
  227.         {
  228.             md.setRate(0);
  229.         }
  230.         catch( QTException e )
  231.         {
  232.             e.printStackTrace();
  233.         }
  234.     }
  235.     
  236.     /**
  237.      *  Plays the sound file associated with the current AnimalPane
  238.      */
  239.     public void playSound()
  240.     {
  241.         if (player == null)
  242.             return;
  243.  
  244.         try
  245.         {
  246.             player.setTime(0);            //Start the sound at the beginning
  247.             player.startTasking();         //Make sure the player gets time to play the sound
  248.             player.setRate(1);             //Start playing
  249.         }
  250.         catch (QTException exc)
  251.         {
  252.             exc.printStackTrace();
  253.         }
  254.     }
  255.     
  256.     /**
  257.      * Creates a Movie from the specified path and adds it
  258.      * to the compositor in the hardcoded location.
  259.      * @param moviePath the relative path to the movie file to add.
  260.      * @param layer, the layer of the compositor to add the movie to.
  261.      * @param x the x coordinate location.
  262.      * @param y the y coordinate location.
  263.      * @see #makeMovie
  264.      */
  265.     protected void addMovie(String moviePath, int layer, int x, int y)
  266.     {
  267.         try
  268.         {
  269.             Movie m = makeMovie (new QTFile (QTFactory.findAbsolutePath (moviePath)));
  270.             md = new MoviePresenter (m);
  271.             md.setLocation (x, y);
  272.             // Add the Movie Controls
  273.             
  274.             playRel     = MakePresenterFromFile( "data/Play.jpg" );            // Play >
  275.             playPress   = MakePresenterFromFile( "data/PlayPressed.jpg" );
  276.             playPlaying = MakePresenterFromFile( "data/Playing.jpg" );
  277.             playButton = new MovieButton( playRel, playPress, playPlaying );
  278.             playButton.setLabel ("Play");
  279.             playButton.setLocation( x + 68, y + 152 );
  280.             
  281.             stopRel      = MakePresenterFromFile( "data/Stop.jpg" );        // Stop []
  282.             stopPress    = MakePresenterFromFile( "data/StopPressed.jpg" );
  283.             stopDeactive = MakePresenterFromFile( "data/Stopped.jpg" );
  284.             stopButton  = new MovieButton( stopRel, stopPress, stopDeactive );
  285.             stopButton.setLabel ("Stop");
  286.             stopButton.setLocation( x + 136, y + 152 );
  287.             
  288.             rewRel      = MakePresenterFromFile( "data/Rewind.jpg" );        // Rewind <<
  289.             rewPress    = MakePresenterFromFile( "data/RewindPressed.jpg" );
  290.             rewDeactive = MakePresenterFromFile( "data/Rewind.jpg" );
  291.             rewindButton = new MovieButton( rewRel, rewPress, rewDeactive );
  292.             rewindButton.setLabel( "Rewind" );
  293.             rewindButton.setLocation( x, y + 152 );
  294.             
  295.             ButtonListener bl = new ButtonListener();                        // create a new listener for the buttons
  296.             
  297.             playButton.addActionListener(bl);
  298.             stopButton.addActionListener(bl);
  299.             rewindButton.addActionListener(bl);
  300.             
  301.             compositor.addMember (md, layer);                                // add the movie presenter
  302.             compositor.addMember (playButton);                                //     and buttons to the compositor
  303.             compositor.addMember (stopButton);
  304.             compositor.addMember (rewindButton);
  305.             
  306.             buttonController = new QTMouseTargetController (false);    
  307.             buttonController.addMember(playButton);
  308.             buttonController.addMember(stopButton);
  309.             buttonController.addMember(rewindButton);
  310.         
  311.             compositor.addController (buttonController);
  312.             buttonActivator = new ButtonActivator();
  313.             
  314.             md.setRate(1);                                                    // start the movie        
  315.         }
  316.         catch (QTException exc)
  317.         {
  318.             exc.printStackTrace();
  319.         }
  320.         catch ( IOException exc )
  321.         {
  322.             exc.printStackTrace();
  323.         }    
  324.     }
  325.     
  326.     /**
  327.      * Utility routine loads an image from a file, renders it, and adds it to the compositor
  328.      * @param filename the name of the file to be displayed
  329.      * @param layer the layer in the compositor where the image will be displayed
  330.      * @param x the x coordinate of the top left corner where the image is to be displayed
  331.      * @param y the y coordinate of the top left corner where the image is to be displayed
  332.      */
  333.     protected void displayImage( String filename, int layer, int x, int y )
  334.     {
  335.         try
  336.         {    
  337.             QTFile file = new QTFile (QTFactory.findAbsolutePath (filename));
  338.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer(file);
  339.             ImagePresenter presenter = ImagePresenter.fromGraphicsImporterDrawer(drawer);
  340.             presenter.setLocation( x, y );
  341.             compositor.addMember(presenter, layer );
  342.         }
  343.         catch (QTException exc)
  344.         {
  345.             exc.printStackTrace();
  346.         }
  347.         catch (IOException exc)
  348.         {
  349.             exc.printStackTrace();
  350.         }        
  351.     }
  352.     
  353.     /**
  354.      * Reads a text file from the disk and displays it in the compositor
  355.      * @param textPath the relative path where the text is located
  356.      * @param layer, the layer of the compositor to add the text to.
  357.      * @param x the x coordinate location.
  358.      * @param y the y coordinate location.
  359.      * @param width the width of the area to display the text.
  360.      * @param height the height of the area to display the text.
  361.      */
  362.     protected void addText(String textPath, int layer, int x, int y, int width, int height)
  363.     {
  364.         try
  365.         {
  366.             TextPresenter text = new TextPresenter(textPath, new Dimension(width, height));
  367.             
  368.             ImagePresenter presenter = text.getPresenter();
  369.             Matrix theMatrix = new Matrix();
  370.             theMatrix.translate( (float) x, (float) y );
  371.             presenter.setMatrix(theMatrix);
  372.             compositor.addMember(presenter, layer);
  373.         }
  374.         catch ( QTException exc )
  375.         {
  376.             exc.printStackTrace();
  377.         }
  378.         catch ( FileNotFoundException exc )
  379.         {
  380.             exc.printStackTrace();
  381.         }    
  382.     }
  383.     
  384.     /**
  385.      * Loads the specified sound file from disk and prepares it for playing
  386.      * @param ???
  387.      */
  388.     protected void loadSound(String soundPath)
  389.     {
  390.         try
  391.         {
  392.             String soundLocation = QTFactory.findAbsolutePath(soundPath).getPath();
  393.             //this call works with a file://, http://, rtsp://located movie
  394.             player = (QTPlayer)QTFactory.makeDrawable ("file://" + soundLocation);
  395.         }
  396.         catch (IOException exc)
  397.         {
  398.             exc.printStackTrace();
  399.         }
  400.         catch (QTException exc)
  401.         {
  402.             exc.printStackTrace();
  403.         }
  404.     }
  405.     
  406.  
  407.     /**
  408.      * Opens the movie file and sets it up to be played.
  409.      * @param f a QTFile representing the movie to initialize.
  410.      * @return the movie, ready to play
  411.      * @see #addMovie
  412.      */
  413.     protected Movie makeMovie (QTFile f) throws IOException, QTException
  414.     {
  415.         OpenMovieFile movieFile = OpenMovieFile.asRead(f);
  416.         Movie m = Movie.fromFile (movieFile);
  417.         m.getTimeBase().setFlags (StdQTConstants.loopTimeBase);    
  418.         return m;    
  419.     }
  420.     
  421.     /**
  422.      *    Creates a TwoDSprite that behaves as a simple rollover button (such as the map button)
  423.      */
  424.     protected TwoDSprite CreateRollover( String filename, int layer, int x, int y )
  425.     {
  426.         TwoDSprite sprite = null;
  427.         try
  428.         {
  429.             QTFile                 file = new QTFile( QTFactory.findAbsolutePath( filename ));
  430.             ImagePresenter         presenter = ImagePresenter.fromFile( file );
  431.             ImageDataSequence     dataSeq = new ImageDataSequence (presenter.getDescription());
  432.             
  433.             dataSeq.addMember( presenter.getImage() );
  434.             
  435.             if ((QTSession.isCurrentOS(QTSession.kWin32) && QTSession.getQTMajorVersion() == 3) == false)    //doesn't work on QT3.0.2 on Win
  436.                 dataSeq = ImageUtil.makeTransparent ( dataSeq, QDColor.white );        // recompress the image using white as the transparent color
  437.             
  438.             Matrix theMatrix = new Matrix();
  439.             theMatrix.translate( (float) x, (float) y );
  440.             sprite = new TwoDSprite( dataSeq, theMatrix, true, 10 );
  441.             
  442.             GraphicsMode normalGM = new GraphicsMode (QDConstants.blend, QDColor.lightGray);
  443.             sprite.setGraphicsMode (normalGM);
  444.             compositor.addMember(sprite, layer);
  445.             ctr.addMember( sprite );
  446.         }
  447.         catch ( QTException exc )
  448.         {
  449.             exc.printStackTrace();
  450.         }
  451.         catch ( IOException exc )
  452.         {
  453.             exc.printStackTrace();
  454.         }
  455.         
  456.         return sprite;    
  457.     }
  458.     
  459.     
  460.     protected QTPlayer player;
  461.     protected MoviePresenter md;
  462.     
  463.     protected ImagePresenter playRel;
  464.     protected ImagePresenter playPress;
  465.     protected ImagePresenter playPlaying;
  466.     protected MovieButton playButton;
  467.             
  468.     protected ImagePresenter stopRel;
  469.     protected ImagePresenter stopPress;
  470.     protected ImagePresenter stopDeactive;
  471.     protected MovieButton stopButton;
  472.     
  473.     protected ImagePresenter rewRel;
  474.     protected ImagePresenter rewPress;
  475.     protected ImagePresenter rewDeactive;
  476.     protected MovieButton rewindButton;
  477.     
  478.     protected QTMouseTargetController buttonController;
  479.     protected ButtonActivator buttonActivator;
  480. }
  481.